package com.party.common.utils;

/**
 * @author: zhaoyongke
 * @create: 2024-11-23 09:06
 * @description: 人脸识别
 */


import com.alibaba.fastjson2.JSONObject;
import com.aliyun.facebody20191230.models.CompareFaceResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import com.aliyun.teautil.Common;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;

public class CompareFace {
    public static com.aliyun.facebody20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        /*
          初始化配置对象com.aliyun.teaopenapi.models.Config
          Config对象存放 AccessKeyId、AccessKeySecret、endpoint等配置
         */
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "facebody.cn-shanghai.aliyuncs.com";
        return new com.aliyun.facebody20191230.Client(config);
    }

    public static void main(String[] args_) throws Exception {

        compareFace("F:/图片3.jpg", "F:/图片3.jpg");
    }

    public static Boolean compareFace(String imageUrlA, String imageUrlB) throws Exception {
        String accessKeyId = "LTAI5t9dtmrxG7mg5VnWxWna";
        String accessKeySecret = "SN74k9IHEWWiupN6NfR4KWLO7zJax9";
        com.aliyun.facebody20191230.Client client = CompareFace.createClient(accessKeyId, accessKeySecret);
        // 场景一，使用本地文件
        InputStream inputStreamA = new FileInputStream(new File(imageUrlA));
        InputStream inputStreamB = new FileInputStream(new File(imageUrlB));
        // 场景二，使用任意可访问的url
//        URL url = new URL("https://viapi-test-bj.oss-cn-beijing.aliyuncs.com/viapi-3.0domepic/facebody/CompareFace/CompareFace-left1.png");
//        InputStream inputStreamB = url.openConnection().getInputStream();
        com.aliyun.facebody20191230.models.CompareFaceAdvanceRequest compareFaceAdvanceRequest = new com.aliyun.facebody20191230.models.CompareFaceAdvanceRequest()
                .setImageURLAObject(inputStreamA)
                .setImageURLBObject(inputStreamB);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            CompareFaceResponse compareFaceResponse = client.compareFaceAdvance(compareFaceAdvanceRequest, runtime);
            // 获取整体结果
            String s = Common.toJSONString(TeaModel.buildMap(compareFaceResponse));
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(compareFaceResponse)));
//            {"headers":{"access-control-allow-origin":"*","date":"Sat, 23 Nov 2024 01:54:52 GMT","content-length":"427","keep-alive":"timeout=25","x-acs-request-id":"32282E29-0B2A-5F35-ACB2-88B2BA033FBA","connection":"keep-alive","content-type":"application/json;charset=utf-8","etag":"4RaYDoSY2j+jun+qsmQzZ4A7","access-control-expose-headers":"*","x-acs-trace-id":"47e5cf3b349e75d0c07f6094687f6e44"},"body":{"RequestId":"32282E29-0B2A-5F35-ACB2-88B2BA033FBA","Data":{"MessageTips":"imageA face size is small;imageB face size is small;","QualityScoreA":94.20971,"QualityScoreB":94.20971,"IsMaskB":0,"IsMaskA":0,"Thresholds":[61.0,69.0,75.0],"Confidence":99.999985,"LandmarksAList":[34,43,51,43,42,52,35,61,50,61],"LandmarksBList":[34,43,51,43,42,52,35,61,50,61],"RectAList":[23,23,37,47],"RectBList":[23,23,37,47]}},"statusCode":200}

            //获取 s的Confidence属性的值
            JSONObject jsonObject = JSONObject.parseObject(s);
            double confidence = jsonObject.getJSONObject("body").getJSONObject("Data").getDouble("Confidence");
            if (confidence < 80) {
                return false;
            } else {
                return true;
            }

        } catch (TeaException teaException) {
            // 获取整体报错信息
            System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
            String errson = com.aliyun.teautil.Common.toJSONString(teaException);
            JSONObject jsonObject = JSONObject.parseObject(errson);
            String message = jsonObject.getString("message");
            System.out.println(message);
            // 获取单个字段
            System.out.println(teaException.getCode());
            return false;
        }


    }
}



